// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.models;

import org.dengues.core.process.CompElement;
import org.dengues.core.process.ICompProcess;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-14 下午05:48:00
 */
public class CompConnectionLabel extends CompElement {

    private static final long serialVersionUID = 1111155425543L;

    private final CompConnection connection;

    public static String PROP_TEXT_CHANGE = "CompConnectionLabel.textChange"; //$NON-NLS-1$

    public static String PROP_LOCATION_CHANGE = "CompConnectionLabel.LOCATIONChange"; //$NON-NLS-1$

    private String connLabel;

    private Point location = new Point(0, 0);

    /**
     * Qiang.Zhang.Adolf@gmail.com CompConnectionLabel constructor comment.
     */
    public CompConnectionLabel(CompConnection connection) {
        this.connection = connection;
        this.connLabel = connection.getName();
        if (connection.getSource() instanceof CompNode) {
            if (((CompNode) connection.getSource()).isExternalNode()) {
                connLabel = connection.getType().toString().toLowerCase() + "(" + connLabel + ")";
            }
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.models.CompElement#getCompName()
     */
    @Override
    public String getCompName() {
        return connLabel;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.models.CompElement#getCompProcess()
     */
    @Override
    public ICompProcess getCompProcess() {
        return connection.getCompProcess();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompElement#getLocation()
     */
    @Override
    public Point getLocation() {
        return this.location;
    }

    public void setLocation(Point location) {
        this.location = location;
        fireStructureChange(PROP_LOCATION_CHANGE, this.location);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompElement#getSize()
     */
    @Override
    public Dimension getSize() {
        return null;
    }

    /**
     * Sets the connLabel.
     * 
     * @param connLabel the connLabel to set
     */
    public void setConnLabel(String connLabel) {
        this.connLabel = connLabel;
        connection.setName(connLabel);
        fireStructureChange(PROP_TEXT_CHANGE, connLabel);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "changeLocation".
     */
    public void changeLocation() {
        Point point = new Point();

        CompElement sourceNode = ((CompElement) connection.getSource());
        int x = sourceNode.getLocation().x;
        CompElement targetNode = ((CompElement) connection.getTarget());
        int x2 = targetNode.getLocation().x;

        int width = x2 > x ? sourceNode.getSize().width : targetNode.getSize().width;
        int abs = Math.abs(x2 - x) / 2 - width / 2;
        int px = x2 > x ? x + abs + width : x2 + abs + width;

        GC gc = new GC(Display.getDefault());
        org.eclipse.swt.graphics.Point stringExtent = gc.stringExtent(getCompName());
        gc.dispose();
        px = px - stringExtent.x / 2;
        point.x = px;

        int y = sourceNode.getLocation().y;
        int y2 = targetNode.getLocation().y;

        int height = y2 > y ? sourceNode.getSize().height : targetNode.getSize().height;
        abs = Math.abs(y2 - y) / 2 - height / 2;
        int py = y2 > y ? y + abs + height : y2 + abs + height;
        py = py - stringExtent.y;
        point.y = py;
        setLocation(point);
    }

}
